﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Summary description for Product
/// </summary>
public class Product
{
    #region"Properties"
    public int MaLaptop { get; set;}
    public String TenLapTop { get; set; }
    public int MaLoai { get; set; }
    public String HinhLon { get; set; }
    public String HinhNho { get; set; }
    public int BaoHanh { get; set; }
    public Decimal Gia { get; set; }
    public String CPU { get; set; }
    public String Ram { get; set; }
    public String HDD { get; set; }
    public String GraphicCard { get; set; }
    public String DVD_RW { get; set; }
    public String CardReader { get; set; }
    public String ManHinh { get; set; }
    public String CardMang { get; set; }
    public String TrongLuong { get; set; }
    public String Pin { get; set; }
    public String HeDieuHanh { get; set; }
    public String ThongTinDacBiet { get; set; }
    public String  Mau { get; set; }
    public Boolean DisContined { get; set; }
    public int BinhChon { get; set; }
    public int DiemBinhChon { get; set; }
    public DateTime NgayNhapLapTop { get; set; }
    #endregion"End Properties"

    #region"Methods"
    /// <summary>
    /// Thêm 1 LapTop vào bảng LapTop
    /// </summary>
    /// <param name="p"></param>
    public static void InsertLapTop(Product p) 
    {
        try
        {
            String StoredProcedureName = "sp_InsertLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaLapTop", p.MaLaptop.ToString());
            d.Add("@TenLapTop", p.TenLapTop);
            d.Add("@MaLoai", p.MaLoai.ToString());
            d.Add("@HinhLon", p.HinhLon);
            d.Add("@HinhNho", p.HinhNho);
            d.Add("@BaoHanh", p.BaoHanh.ToString());
            d.Add("@Gia", p.Gia.ToString());
            d.Add("@CPU", p.CPU);
            d.Add("@Ram", p.Ram);
            d.Add("@HDD", p.HDD);
            d.Add("@GraphicCard", p.GraphicCard);
            d.Add("@DVD_RW", p.DVD_RW);
            d.Add("@CardReader", p.CardReader);
            d.Add("@ManHinh", p.ManHinh);
            d.Add("@CardMang", p.CardMang);
            d.Add("@TrongLuong", p.TrongLuong);
            d.Add("@Pin", p.Pin);
            d.Add("@HeDieuHanh", p.HeDieuHanh);
            d.Add("@ThongTinDacBiet", p.ThongTinDacBiet);
            d.Add("@Mau", p.Mau);
            d.Add("@DisContined", p.DisContined.ToString());
            d.Add("@BinhChon", p.BinhChon.ToString());
            d.Add("@DiemBinhChon", p.DiemBinhChon.ToString());
            d.Add("@NgayNhapLapTop", p.NgayNhapLapTop.ToShortDateString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }

    }
    /// <summary>
    /// Cập nhập 1 LapTop
    /// </summary>
    /// <param name="p"></param>
    public static void UpdateLapTop(Product p) 
    {
        try
        {
            String StoredProcedureName = "sp_UpdateLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaLapTop", p.MaLaptop.ToString());
            d.Add("@TenLapTop", p.TenLapTop);
            d.Add("@MaLoai", p.MaLoai.ToString());
            d.Add("@HinhLon", p.HinhLon);
            d.Add("@HinhNho", p.HinhNho);
            d.Add("@BaoHanh", p.BaoHanh.ToString());
            d.Add("@Gia", p.Gia.ToString());
            d.Add("@CPU", p.CPU);
            d.Add("@Ram", p.Ram);
            d.Add("@HDD", p.HDD);
            d.Add("@GraphicCard", p.GraphicCard);
            d.Add("@DVD_RW", p.DVD_RW);
            d.Add("@CardReader", p.CardReader);
            d.Add("@ManHinh", p.ManHinh);
            d.Add("@CardMang", p.CardMang);
            d.Add("@TrongLuong", p.TrongLuong);
            d.Add("@Pin", p.Pin);
            d.Add("@HeDieuHanh", p.HeDieuHanh);
            d.Add("@ThongTinDacBiet", p.ThongTinDacBiet);
            d.Add("@Mau", p.Mau);
            d.Add("@DisContined", p.DisContined.ToString());
            d.Add("@BinhChon", p.BinhChon.ToString());
            d.Add("@DiemBinhChon", p.DiemBinhChon.ToString());
            d.Add("@NgayNhapLapTop", p.NgayNhapLapTop.ToShortDateString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }

    }
    /// <summary>
    /// Xóa 1 LapTop
    /// </summary>
    /// <param name="p"></param>
    public static void DeleteLaptop(Product p) 
    {
        try
        {
            String StoredProcedureName = "sp_DeleteLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaLapTop", p.MaLaptop.ToString());
            DataAccess.ExecuteNonQuery(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    /// <summary>
    /// Lấy tất cả danh sách LapTop
    /// </summary>
    /// <returns>List</returns>
    public List<Product> GetAllLapTopList()
    {
        try
        {
            List<Product> list = new List<Product>();
            String StoredProcedureName = "sp_GetAllLapTopList";
            String connectionString = DataAccess.ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand comd = new SqlCommand(StoredProcedureName, con);
            con.Open();
            SqlDataReader datareader = comd.ExecuteReader(CommandBehavior.CloseConnection);
            if (datareader.HasRows)
            {
                while (datareader.Read())
                {
                    Product p = new Product()
                    {
                        MaLaptop = int.Parse(datareader["MaLapTop"].ToString()),
                        TenLapTop = datareader.GetString(1),
                        MaLoai = int.Parse(datareader["MaLoai"].ToString()),
                        HinhLon = datareader.GetString(3),
                        HinhNho = datareader.GetString(4),
                        BaoHanh = int.Parse(datareader["BaoHanh"].ToString()),
                        Gia = decimal.Parse(datareader["Gia"].ToString()),
                        CPU = datareader.GetString(7),
                        Ram = datareader.GetString(8),
                        HDD = datareader.GetString(9),
                        GraphicCard = datareader.GetString(10),
                        DVD_RW = datareader.GetString(11),
                        CardReader = datareader.GetString(12),
                        ManHinh = datareader.GetString(13),
                        CardMang = datareader.GetString(14),
                        TrongLuong = datareader.GetString(15),
                        Pin = datareader.GetString(16),
                        HeDieuHanh = datareader.GetString(17),
                        ThongTinDacBiet = datareader.GetString(18),
                        Mau = datareader.GetString(19),
                        DisContined = bool.Parse(datareader["DisContined"].ToString()),
                        BinhChon = int.Parse(datareader["BinhChon"].ToString()),
                        DiemBinhChon = int.Parse(datareader["DiemBinhChon"].ToString()),
                        NgayNhapLapTop = DateTime.Parse(datareader["NgayNhapLapTop"].ToString())

                    };
                    list.Add(p);
                }
            }
            return list;
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy danh sách tất cả LapTop theo MaLoaiLapTop
    /// </summary>
    /// <param name="MaLoaiLaptop"></param>
    /// <returns></returns>
    public static DataTable GetLapTopByCategory(int MaLoaiLaptop)
    {
        try
        {
            String StoredProcedureName = "sp_GetLapTopByCategory";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaLoaiLapTop", MaLoaiLaptop.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy danh sách các LapTop theo TenLapTop
    /// </summary>
    /// <param name="TenLaptop"></param>
    /// <returns></returns>
    public static DataTable GetLapTopByName(String TenLaptop) 
    {
        try
        {
            String StoredProcedureName = "sp_GetLapTopByName";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@TenLapTop", TenLaptop);
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy danh sách LapTop theo giá
    /// </summary>
    /// <param name="priceStart"></param>
    /// <param name="priceEnd"></param>
    /// <returns></returns>
    public static DataTable GetLapTopByPrice(decimal priceStart, decimal priceEnd)
    {
        try
        {
            String StoredProcedureName = "sp_GetLapTopByPrice";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@priceStart", priceStart.ToString());
            d.Add("@priceEnd", priceEnd.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy số LapTop mới
    /// </summary>
    /// <param name="Quantity">Số lượng</param>
    /// <returns></returns>
    public static DataTable GetNewLapTop(int Quantity)
    {
        try
        {
            String StoredProcedureName = "sp_GetNewLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@Quantity", Quantity.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy danh sách LapTop bán chạy nhất
    /// </summary>
    /// <param name="Quantity">Số lượng</param>
    /// <returns></returns>
    public static DataTable GetLapTopBestSale(int Quantity) 
    {
        try
        {
            String StoredProcedureName = "sp_GetLapTopBestSales";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@Quantity", Quantity.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /// <summary>
    /// Lấy danh sách LapTop được bình chọn nhiều nhất
    /// </summary>
    /// <param name="Quantity"></param>
    /// <returns></returns>
    public static DataTable GetFavoriteLapTop(int Quantity)
    {
        try
        {
            String StoredProcedureName = "sp_GetFavoriteLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@Quantity", Quantity.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public DataTable UpdateVoteLapTop(int MaLapTop, int DiemBinhChon)
    {
        try
        {
            String StoredProcedureName = "sp_UpdateVoteLapTop";
            IDictionary<String, String> d = new Dictionary<String, String>();
            d.Add("@MaLapTop", MaLapTop.ToString());
            d.Add("@DiemBinhChon",DiemBinhChon.ToString());
            return DataAccess.GetList(StoredProcedureName, d);
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    #endregion"End Methods"
}
